﻿/*
 * 2023-06-16 PconSendMsg新增字段Number，Response
 * 2023-08-11 PconRecvMsg新增字段Number，Response
 * Number为递增数
 * Response为设备处理完后需要跟Number相同
 */
using HamcoWcs.Models.WcsLocations;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace HamcoWcs.Models.DeviceMsgs.SrmMsgs
{
    public class SrmRecvMsg : MsgBase
    {
        #region [Constructor]
        public SrmRecvMsg() : base(true)
        {

        }
        public SrmRecvMsg(bool isReserve = true) : base(isReserve)
        {
        }
        public SrmRecvMsg(byte[] msg, bool isReserve = true) : base(isReserve)
        {
            DeviceMode = Reserve(BitConverter.ToUInt16(msg, 0));
            ErrorCode = Reserve(BitConverter.ToUInt16(msg, 2));
            IsTask = Reserve(BitConverter.ToUInt16(msg, 4));
            IsGoods = Reserve(BitConverter.ToUInt16(msg, 6));
            UnitId = Encoding.ASCII.GetString(msg, 8, 18).Trim('\0').Trim();
            CurrentBay = Reserve(BitConverter.ToUInt16(msg, 26));
            Enabled = Reserve(BitConverter.ToUInt16(msg, 28));
            DestinationBay = Reserve(BitConverter.ToUInt16(msg, 30));
            ServoState = Reserve(BitConverter.ToUInt16(msg, 32));
            Speed = BitConverter.ToUInt32(Reserve(msg.Skip(34).Take(4).ToArray()));
            BcrState = Reserve(BitConverter.ToUInt16(msg, 38));
            IOInfo = Reserve(msg.Skip(40).Take(12).ToArray());
            Request = Reserve(BitConverter.ToUInt16(msg, 52));
            Number = Reserve(BitConverter.ToUInt16(msg, 54));
            Response = Reserve(BitConverter.ToUInt16(msg, 56));
            if (UnitId == null) UnitId = "";
        }

        #endregion
        public ushort DeviceMode { get; set; }
        public ushort ErrorCode { get; set; }
        public ushort IsTask { get; set; }
        public ushort IsGoods { get; set; }
        public string UnitId { get; set; } = "";
        public ushort CurrentBay { get; set; }
        public ushort Enabled { get; set; }
        public ushort DestinationBay { get; set; }
        //public ushort LiftCount { get; set; }//20230627
        public ushort ServoState { get; set; }
        public UInt32 Speed { get; set; }
        public ushort BcrState { get; set; }
        public byte[] IOInfo { get; set; }
        public ushort Request { get; set; }
        public ushort Number { get; set; }      //2023-08-11 新增
        public ushort Response { get; set; }    //2023-08-11 新增

    }
    public class SrmSendMsg : MsgBase
    {
        public ushort FromStation { get; set; }
        public ushort FromRow { get; set; }
        public ushort FromBay { get; set; }
        public ushort FromLev { get; set; }
        public ushort FromDeep { get; set; }
        public ushort ToStation { get; set; }
        public ushort ToRow { get; set; }
        public ushort ToBay { get; set; }
        public ushort ToLev { get; set; }
        public ushort ToDeep { get; set; }
        public ushort Size { get; set; }
        public string UnitId { get; set; } = "";
        public ushort Request { get; set; }
        public ushort Number { get; set; }      //2023-06-16 新增
        public ushort Response { get; set; }    //2023-06-16 新增

        public SrmSendMsg() : base(true)
        {

        }
        public SrmSendMsg(bool isReserve = true) : base(isReserve)
        {
        }
        public SrmSendMsg(byte[] msg, bool isReserve = true) : base(isReserve)
        {
            FromStation = Reserve(BitConverter.ToUInt16(msg, 0));
            FromRow = Reserve(BitConverter.ToUInt16(msg, 2));
            FromBay = Reserve(BitConverter.ToUInt16(msg, 4));
            FromLev = Reserve(BitConverter.ToUInt16(msg, 6));
            FromDeep = Reserve(BitConverter.ToUInt16(msg, 8));
            ToStation = Reserve(BitConverter.ToUInt16(msg, 10));
            ToRow = Reserve(BitConverter.ToUInt16(msg, 12));
            ToBay = Reserve(BitConverter.ToUInt16(msg, 14));
            ToLev = Reserve(BitConverter.ToUInt16(msg, 16));
            ToDeep = Reserve(BitConverter.ToUInt16(msg, 18));
            Size = Reserve(BitConverter.ToUInt16(msg, 20));
            UnitId = Encoding.ASCII.GetString(msg, 22, 18).Trim('\0').Trim();
            Request = Reserve(BitConverter.ToUInt16(msg, 40));
            Number = Reserve(BitConverter.ToUInt16(msg, 42));
            Response = Reserve(BitConverter.ToUInt16(msg, 44));
            if (UnitId == null) UnitId = "";
        }

        public SrmSendMsg(LocStationInfo fromStation, LocStationInfo toStation, bool isReserve = true) : base(isReserve)
        {
            FromStation = fromStation.Station;
            FromRow = fromStation.Row;
            FromBay = fromStation.Bay;
            FromLev = fromStation.Lev;
            FromDeep = fromStation.Deep;
            ToStation = toStation.Station;
            ToRow = toStation.Row;
            ToBay = toStation.Bay;
            ToLev = toStation.Lev;
            ToDeep = toStation.Deep;
        }

        public byte[] Encode()
        {
            var bytes = new List<byte>();
            bytes.AddRange(BitConverter.GetBytes(Reserve(FromStation)));
            bytes.AddRange(BitConverter.GetBytes(Reserve(FromRow)));
            bytes.AddRange(BitConverter.GetBytes(Reserve(FromBay)));
            bytes.AddRange(BitConverter.GetBytes(Reserve(FromLev)));
            bytes.AddRange(BitConverter.GetBytes(Reserve(FromDeep)));
            bytes.AddRange(BitConverter.GetBytes(Reserve(ToStation)));
            bytes.AddRange(BitConverter.GetBytes(Reserve(ToRow)));
            bytes.AddRange(BitConverter.GetBytes(Reserve(ToBay)));
            bytes.AddRange(BitConverter.GetBytes(Reserve(ToLev)));
            bytes.AddRange(BitConverter.GetBytes(Reserve(ToDeep)));
            bytes.AddRange(BitConverter.GetBytes(Reserve(Size)));
            bytes.AddRange(Encoding.ASCII.GetBytes(UnitId.PadRight(18, ' ')));
            bytes.AddRange(BitConverter.GetBytes(Reserve(Request)));
            bytes.AddRange(BitConverter.GetBytes(Reserve(Number)));
            bytes.AddRange(BitConverter.GetBytes(Reserve(Response)));
            return bytes.ToArray();
        }
    }
}
